import { getChannelList, getAllChannelList } from '@/api/channels'
export default {
  namespaced: true,
  state: {
    channelList: [],
    AllChannelList: []
  },
  mutations: {
    setChannelList (state, payload) {
      state.channelList = payload
    },
    setAllChannelList (state, payload) {
      state.AllChannelList = payload
    },
    addChannel (state, payload) {
      state.channelList.push(payload)
    },
    delChannel (state, channel) {
      state.channelList = state.channelList.filter(item => item.id !== channel.id)
    }
  },
  actions: {
    async getChannelList (context) {
      const res = await getChannelList()
      console.log(res.data.channels)
      context.commit('setChannelList', res.data.channels)
    },
    async getAllChannelList (context) {
      const res = await getAllChannelList()
      context.commit('setAllChannelList', res.data.channels)
    }
  },
  getters: {
    optionalChannelList (state) {
      return state.AllChannelList
        .filter(item => state.channelList.findIndex(v => +v.id === +item.id) === -1)
    }

  }
}
